package com.hex.es.service.impl;

import com.hex.es.annotation.EsBuildParamTypeInfo;
import com.hex.es.service.ExecutionLinkControllerInterface;
import com.hex.es.service.ParamBuildInterface;
import com.hex.es.service.ParamTypeCheckInterface;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description: 基本类型处理器
 * @author: Hex
 * @date: 2019/11/13 16:29
 */
@Slf4j
@Service
public class ParamBaseTypeCheckImpl implements ParamTypeCheckInterface {

    @Resource
    private ExecutionLinkControllerInterface controllerInterface;

    @Override
    public void handler(String paramName,Object paramValue, BoolQueryBuilder boolQueryBuilder, NativeSearchQueryBuilder searchQueryBuilder, EsBuildParamTypeInfo esBuildParamTypeInfo) throws IllegalAccessException {
        if (paramValue != null) {
            controllerInterface.paramBuild(this.getClass(),paramName,paramValue,boolQueryBuilder,searchQueryBuilder,esBuildParamTypeInfo);
        }
    }

    @Override
    public Boolean checkType(Object object) {
        Class className = object.getClass();
        if (className.equals(Integer.class) ||
                className.equals(Byte.class) ||
                className.equals(Long.class) ||
                className.equals(Double.class) ||
                className.equals(Float.class) ||
                className.equals(Character.class) ||
                className.equals(Short.class) ||
                className.equals(Boolean.class) ||
                className.equals(String.class) ||
                className.equals(Date.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Map<Class, List<Class<? extends ParamBuildInterface>>> build() {
        Map<Class, List<Class<? extends ParamBuildInterface>>> maps = new HashMap<>(16);
        List<Class<? extends ParamBuildInterface>> classesList = Arrays.asList(ParamBuildPageImpl.class, ParamBuildOrderByImpl.class, ParamBuildRangeImpl.class, ParamBuildGorupByImpl.class, ParamBuildBaseImpl.class);
        maps.put(this.getClass(),classesList);
        return maps;
    }
}
